﻿package org.xhs.report.txt.ui.panel
{
    import __AS3__.vec.*;
    import baidu.ui.managers.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.utils.*;
    import baidu.wenku.txt.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.text.*;

    public class BgColorPanel extends Sprite implements IPanel
    {
        protected var _isOpen:Boolean = false;
        protected var hasInited:Boolean = false;
        protected var hasDestroyed:Boolean = true;
        protected var itemWidth:Number = 153;
        protected var itemHeight:Number = 21;
        protected var bgColorPanel:Sprite;
        protected var bgColorSelected:Sprite;
        protected var screenModel:ScreenModel;
        protected var itemArray:Vector.<Sprite>;
        public static const BG_COLOR_CHANGED:String = "BG_COLOR_CHANGED";

        public function BgColorPanel()
        {
            this.init();
            return;
        }// end function

        protected function init() : void
        {
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.screenModel.addEventListener(Event.RESIZE, this.screenResize);
            return;
        }// end function

        protected function initPanel() : void
        {
            var _loc_2:int = 0;
            var _loc_3:Sprite = null;
            var _loc_4:TextField = null;
            if (this.hasInited)
            {
                return;
            }
            graphics.clear();
            graphics.lineStyle(2, 16777215, 0.4);
            graphics.beginFill(0, 0.4);
            graphics.moveTo(1, 5);
            graphics.lineTo(125, 5);
            graphics.lineTo(130, 0);
            graphics.lineTo(135, 5);
            graphics.lineTo(174, 5);
            graphics.lineTo(174, 25 + TxtConfig.BG_COLOR_LIST.length * this.itemHeight);
            graphics.lineTo(1, 25 + TxtConfig.BG_COLOR_LIST.length * this.itemHeight);
            graphics.lineTo(1, 5);
            graphics.endFill();
            filters = [new DropShadowFilter(2, 45, 0, 0.4, 4, 4)];
            this.bgColorSelected = new Sprite();
            this.bgColorSelected.graphics.lineStyle(1, 16777215, 0.4);
            this.bgColorSelected.graphics.beginFill(0, 0.4);
            this.bgColorSelected.graphics.drawRect(0, 0, this.itemWidth, this.itemHeight);
            this.bgColorSelected.graphics.endFill();
            this.bgColorSelected.x = 12;
            addChild(this.bgColorSelected);
            var _loc_1:* = TxtConfig.BG_COLOR_LIST.length;
            this.itemArray = new Vector.<Sprite>;
            _loc_2 = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = new Sprite();
                _loc_3.name = "item_" + _loc_2;
                _loc_3.graphics.beginFill(16711680, 0);
                _loc_3.graphics.drawRect(0, 0, this.itemWidth, this.itemHeight);
                _loc_3.graphics.beginFill(TxtConfig.BG_COLOR_LIST[_loc_2].bgColor, 1);
                _loc_3.graphics.drawRoundRect(16, 4, 13, 13, 4, 4);
                _loc_3.graphics.endFill();
                _loc_4 = new TextField();
                _loc_4.autoSize = "left";
                _loc_4.defaultTextFormat = new TextFormat(null, 12, 16777215);
                _loc_4.text = TxtConfig.BG_COLOR_LIST[_loc_2].name;
                _loc_4.x = 40;
                _loc_4.y = (_loc_3.height - _loc_4.height) / 2;
                _loc_3.addChild(_loc_4);
                _loc_3.buttonMode = true;
                _loc_3.mouseChildren = false;
                _loc_3.addEventListener(MouseEvent.MOUSE_OVER, this.bgColorItemMouseHandler);
                _loc_3.addEventListener(MouseEvent.MOUSE_OUT, this.bgColorItemMouseHandler);
                _loc_3.addEventListener(MouseEvent.MOUSE_DOWN, this.bgColorItemMouseHandler);
                this.itemArray.push(_loc_3);
                _loc_3.x = 12;
                _loc_3.y = 17 + _loc_2 * this.itemHeight;
                addChild(_loc_3);
                _loc_2++;
            }
            this.hasInited = true;
            return;
        }// end function

        protected function screenResize(event:Event) : void
        {
            this.resetPanel();
            return;
        }// end function

        public function resetPanel() : void
        {
            if (!this._isOpen)
            {
                return;
            }
            this.screenModel.addStageMask();
            this.initPanel();
            this.bgColorSelected.y = 17 + WenkuSoVo.bgColorIndex * this.itemHeight;
            x = this.screenModel.stageWidth - 210;
            y = Config.TB_HEIGHT;
            this.screenModel.stage.addChild(this);
            if (this.screenModel.stage.hasEventListener(MouseEvent.CLICK))
            {
                this.screenModel.stage.removeEventListener(MouseEvent.CLICK, this.bgColorStageClick);
            }
            this.screenModel.stage.addEventListener(MouseEvent.CLICK, this.bgColorStageClick);
            this.hasDestroyed = false;
            return;
        }// end function

        protected function bgColorItemMouseHandler(event:MouseEvent) : void
        {
            var _loc_2:* = event.target.name;
            var _loc_3:* = int(_loc_2.substr((_loc_2.length - 1)));
            var _loc_4:* = event.target.width;
            var _loc_5:* = event.target.height;
            var _loc_6:* = Sprite(event.target).graphics;
            switch(event.type)
            {
                case MouseEvent.MOUSE_OVER:
                {
                    _loc_6.clear();
                    _loc_6.beginFill(16777215, 0.2);
                    _loc_6.drawRect(0, 0, _loc_4, _loc_5);
                    _loc_6.beginFill(TxtConfig.BG_COLOR_LIST[_loc_3].bgColor, 1);
                    _loc_6.drawRoundRect(16, 4, 13, 13, 4, 4);
                    _loc_6.endFill();
                    break;
                }
                case MouseEvent.MOUSE_OUT:
                {
                    _loc_6.clear();
                    _loc_6.beginFill(16777215, 0);
                    _loc_6.drawRect(0, 0, _loc_4, _loc_5);
                    _loc_6.beginFill(TxtConfig.BG_COLOR_LIST[_loc_3].bgColor, 1);
                    _loc_6.drawRoundRect(16, 4, 13, 13, 4, 4);
                    _loc_6.endFill();
                    break;
                }
                case MouseEvent.MOUSE_DOWN:
                {
                    if (WenkuSoVo.bgColorIndex == _loc_3)
                    {
                        return;
                    }
                    WenkuSoVo.bgColorIndex = Math.min(_loc_3, (TxtConfig.BG_COLOR_LIST.length - 1));
                    dispatchEvent(new Event(BgColorPanel.BG_COLOR_CHANGED));
                    WenkuLog.addLog(WenkuLog.LOGURL_BGCOLOR + int((WenkuSoVo.bgColorIndex + 1)));
                    this.isOpen = false;
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            event.stopPropagation();
            return;
        }// end function

        protected function bgColorStageClick(event:MouseEvent = null) : void
        {
            this.isOpen = false;
            if (event)
            {
                event.stopPropagation();
            }
            return;
        }// end function

        public function destroy() : void
        {
            var _loc_2:DisplayObject = null;
            if (this.hasDestroyed)
            {
                return;
            }
            if (this.screenModel.stage.hasEventListener(MouseEvent.CLICK))
            {
                this.screenModel.stage.removeEventListener(MouseEvent.CLICK, this.bgColorStageClick);
            }
            var _loc_1:int = 0;
            while (_loc_1 < this.itemArray.length)
            {
                
                (this.itemArray[_loc_1] as Sprite).removeEventListener(MouseEvent.MOUSE_OVER, this.bgColorItemMouseHandler);
                (this.itemArray[_loc_1] as Sprite).removeEventListener(MouseEvent.MOUSE_OUT, this.bgColorItemMouseHandler);
                (this.itemArray[_loc_1] as Sprite).removeEventListener(MouseEvent.MOUSE_DOWN, this.bgColorItemMouseHandler);
                _loc_1++;
            }
            this.itemArray = null;
            while (this.numChildren > 0)
            {
                
                _loc_2 = getChildAt(0);
                this.removeChildAt(0);
                _loc_2 = null;
            }
            this.screenModel.removeStageMask();
            if (this.screenModel.stage.contains(this))
            {
                this.screenModel.stage.removeChild(this);
            }
            this.hasInited = false;
            this.hasDestroyed = true;
            return;
        }// end function

        public function set isOpen(param1:Boolean) : void
        {
            this._isOpen = param1;
            if (this._isOpen)
            {
                this.resetPanel();
            }
            else
            {
                this.destroy();
            }
            return;
        }// end function

    }
}
